package com.dsbj.mp.icardservice.controller;

import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.entity.ConsumeInfo;
import com.dsbj.mp.icardservice.entity.Employee;
import com.dsbj.mp.icardservice.entity.EmployeeMonthMeal;
import com.dsbj.mp.icardservice.model.MpUser;
import com.dsbj.mp.icardservice.service.ConsumeInfoService;
import com.dsbj.mp.icardservice.service.EmployeeMonthMealService;
import com.dsbj.mp.icardservice.service.EmployeeService;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.JWSService;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.WxUser;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/consumeInfo")
public class ConsumeInfoController {

	@Resource
	private EmployeeMonthMealService employeeMonthMealService;

	@Resource
	private ConsumeInfoService consumeInfoService;

	@Resource
	private JWSService jwsService;

	@Resource
	private EmployeeService employeeService;


	/**
	 * 根据工号查询消费补助月结
	 * @param
	 * @return
	 * wcm
	 */
	@RequestMapping("/getMonthComsumeInfoByMap")
	public ResponseMessage getMonthComsumeInfoByMap(HttpServletRequest request,String yearmonth) {
		//yearmonth  201901
		MpUser wxUser=(MpUser)request.getAttribute("wxUser");
		String creatorEmployeeNo = wxUser.getEmployeeNo();
		Map<String ,Object> map = new HashMap<String, Object>();
		Employee employee = employeeService.getEmployeeByNo(creatorEmployeeNo);
		EmployeeMonthMeal employeeMonthMeal = null;
		map.put("employeeNo",creatorEmployeeNo);
		if(StringUtils.isNotBlank(yearmonth)){
			int year = Integer.parseInt(yearmonth.substring(0, 4));
			int month = Integer.parseInt(yearmonth.substring(4));
			map.put("yearId",year);
			map.put("monthId",month);
			employeeMonthMeal = employeeMonthMealService.getByMap(map);
		}
		/*if( null == employeeMonthMeal){
			return ResponseMessage.ok(employee);
		}else{*/
			return ResponseMessage.ok(employeeMonthMeal);
		//}
	}

	/**
	 * 个人消费考勤月
	 */
	@RequestMapping("/getEmployeeComsumeInfoByMap")
	public ResponseMessage getEmployeeComsumeInfoByMap(HttpServletRequest request,String yearmonth) {
		//yearmonth  201901
		MpUser wxUser=(MpUser)request.getAttribute("wxUser");
		String creatorEmployeeNo = wxUser.getEmployeeNo();
		Map<String ,Object> map = new HashMap<String, Object>();
		List<ConsumeInfo> list = new ArrayList<>();
		//查询考勤默认
        Integer isdefultstartend = consumeInfoService.getIsdefaultstartend(wxUser.getCompanyno());
		if(StringUtils.isNotBlank(yearmonth)){
			map.put("employeeNo",creatorEmployeeNo);
			map.put("companyno",wxUser.getCompanyno());
			map.put("consumeDate", yearmonth);
			map.put("isdefaultstartend", isdefultstartend);
			list = consumeInfoService.getByMap(map);
		}
		return ResponseMessage.ok(list);
	}

	/***
	 * 一周内个人消费数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/getLineData")
	public ResponseMessage getLineData(HttpServletRequest request,String yearmonth) {
		//yearmonth  201901
		MpUser wxUser=(MpUser)request.getAttribute("wxUser");
		String creatorEmployeeNo = wxUser.getEmployeeNo();
		List<ConsumeInfo> list = new ArrayList<>();
		String sysDataYearMonth = consumeInfoService.getSysdateYearMonth();
		Integer isdefultstartend = consumeInfoService.getIsdefaultstartend(wxUser.getCompanyno());
		if(null != yearmonth){
			if(yearmonth.equals(sysDataYearMonth)){
				//用户选择的月份和当前月相同，显示一周内消费数据
				list = consumeInfoService.getEmployeeWeekConsumeInfo(creatorEmployeeNo);
			}else{
				//用户选择的月份和当前月份不同，显示前三个月消费数据
				list = getEmployeeThreeMonthConsumeInfo(creatorEmployeeNo,yearmonth,wxUser.getCompanyno(),isdefultstartend);
			}
		}
		return ResponseMessage.ok(list);
	}

	/***
	 * 3个月前内个人消费数据
	 * @return
	 */
	public List<ConsumeInfo> getEmployeeThreeMonthConsumeInfo(String EmployeeNo, String yearmonth,String companyno,Integer isdefultstartend) {
		Map<String ,Object> map = new HashMap<String, Object>();
		map.put("consumeDate",yearmonth);
		map.put("employeeno",EmployeeNo);
		map.put("companyno",companyno);
		map.put("isdefaultstartend",isdefultstartend);
		List<ConsumeInfo> list = consumeInfoService.getEmployeeThreeMonthConsumeInfo(map);
		return list;
	}

	/****
	 * 获取了累计消费
	 * @param request
	 * @param yearmonth
	 * @return
	 */
	@RequestMapping("/getConsumeMoneyData")
	public ResponseMessage getConsumeMoneyData(HttpServletRequest request,String yearmonth) {
		//yearmonth  201901
		MpUser wxUser=(MpUser)request.getAttribute("wxUser");
		String creatorEmployeeNo = wxUser.getEmployeeNo();
		Map<String ,Object> map = new HashMap<String, Object>();
		BigDecimal b = new BigDecimal(0);
		//查询考勤默认
		Integer isdefaultstartend = consumeInfoService.getIsdefaultstartend(wxUser.getCompanyno());
		if(StringUtils.isNotBlank(yearmonth)){
			map.put("employeeNo",creatorEmployeeNo);
			map.put("companyno",wxUser.getCompanyno());
			map.put("consumeDate", yearmonth);
			map.put("isdefaultstartend", isdefaultstartend);
			b = consumeInfoService.getConsumeMoney(map);
		}
		return ResponseMessage.ok(b);
	}
	/**
	 *
	 * @param
	 * @return
	 * @author wcm 2019-12-09
	 */
	public static void main(String[] args){

	}

}
